from django.db import models
from user.models import CustomUser
from main.models import Article
# Create your models here.


class Collect(models.Model):
    user = models.ForeignKey(verbose_name="用户", to=CustomUser, on_delete=models.CASCADE)
    article = models.ForeignKey(verbose_name="文章", to=Article, on_delete=models.CASCADE)
    create_time = models.DateTimeField(verbose_name="收藏时间", auto_now_add=True)

    def __str__(self):
        return f"用户:{self.user.username}收藏   文章:{self.article.title}"


    class Meta:
        unique_together = ('user', 'article')


class Comment(models.Model):
    article = models.ForeignKey(verbose_name="文章", to=Article, on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name="用户", to=CustomUser, on_delete=models.CASCADE)
    create_time = models.DateTimeField(verbose_name="评论时间", auto_now_add=True)
    content = models.TextField(verbose_name="评论内容")
    parent = models.ForeignKey(verbose_name="父评论", to='Comment', on_delete=models.CASCADE, null=True, blank=True)
    def __str__(self):
        return f'{self.article.title + "----" + self.user.username + "----" + str(self.id) +"----" + str("None" if not self.parent else self.parent.id)}'


    def get_replays(self):
        return self.comment_set.all()
